﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using BCDataCenter;
using BCDataCenter.BOL;
using System.Data.SqlClient;

public partial class WebUserControl_InPhieuXuatKho : System.Web.UI.UserControl
{
    string error = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        FillGridHoaDonChuaIn();
    }

    private void FillGridHoaDonChuaIn()
    {
        try
        {
            if (Session["Sitecode"] != null)
            {
                BCDataCenter.DataAccessLayer.DAL dtt = new BCDataCenter.DataAccessLayer.DAL();
                DataSet ds = new DataSet();
                ds = dtt.MyExecuteQuery(ref error, "P_GetInvoicePrinted", CommandType.StoredProcedure, new SqlParameter("@Sitecode", Session["Sitecode"].ToString()));
                //BWNhapHoaDonBanHang obj = new BWNhapHoaDonBanHang();
                DataTable dt = ds.Tables[0]; //obj.dsHoaDonChuaIn(ref error, Session["Sitecode"].ToString()).Tables[0];
                if (dt.Rows.Count > 0)
                {
                    gvDonHangChuaIn.DataSource = dt;
                    gvDonHangChuaIn.DataBind();                    
                }
                else
                {
                    dt.Rows.Add(dt.NewRow());
                    gvDonHangChuaIn.DataSource = dt;
                    gvDonHangChuaIn.DataBind();
                    
                    int TotalColumns = gvDonHangChuaIn.Rows[0].Cells.Count;
                    gvDonHangChuaIn.Rows[0].Cells.Clear();
                    gvDonHangChuaIn.Rows[0].Cells.Add(new TableCell());
                    gvDonHangChuaIn.Rows[0].Cells[0].ColumnSpan = TotalColumns;
                    gvDonHangChuaIn.Rows[0].Cells[0].Text = "Không có phiếu xuất kho chưa in.";
                }

            }
        }
        catch { return; }
    }
    protected void gvDonHangChuaIn_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        try
        {
            GridViewRow gvr = ((Control)e.CommandSource).NamingContainer as GridViewRow;
            if (e.CommandName.Equals("Print"))
            {
                Session["MaHDPrinte"] = gvr.Cells[1].Text;
                BCDataCenter.DataAccessLayer.DAL dt = new BCDataCenter.DataAccessLayer.DAL();
                dt.MyExecuteNonQuerys(String.Format("update ARHDR set inpass ='True' where tranno ='{0}'", Session["MaHDPrinte"]), CommandType.Text, ref error);
                FillGridHoaDonChuaIn();
                new BWNhapHoaDonBanHang().UpdateHoaDonChuaIn(ref error, gvr.Cells[1].Text);
                FillGridHoaDonChuaIn();                
                System.Text.StringBuilder sb = new System.Text.StringBuilder();
                sb.Append("window.open('" + Globals.ApplicationPath + "WebMaster/Administrator/InPhieuXuatKho/Print.aspx','PrintMe', 'height=900px,width=900px,scrollbars=1');");
                ScriptManager.RegisterClientScriptBlock(this.UpdatePanel1, this.UpdatePanel1.GetType(), "NewClientScript", sb.ToString(), true);
                //uscMsgBox1.AddMessage("Chức năng In đang cập nhật.", uscMsgBox.enmMessageType.Info);
                
            }
        }
        catch
        {
            uscMsgBox1.AddMessage("Load dữ liệu không được.\n Error: " + error, uscMsgBox.enmMessageType.Error);
            return;
        }
    }
    
}
